package com.trs.zk.config.mybatis;

import com.trs.zk.common.config.Global;
import com.trs.zk.common.entity.BaseEntity;
import com.trs.zk.common.persistence.annotation.MyBatisDao;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import javax.sql.DataSource;

/**
 * @author msy
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2018/7/2716:11
 */
@Configuration
public class MybatisConfig {

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setConfigLocation(new ClassPathResource("mybatis/mybatis-config.xml"));
        bean.setTypeAliasesPackage(Global.getProperty("mybatis.scanBasePackage"));
        bean.setTypeAliasesSuperType(BaseEntity.class);
        bean.setMapperLocations(com.msy.common.io.ResourceUtils.getResources("classpath:/mapper/**/*.xml"));
        bean.setTransactionFactory(new SpringManagedTransactionFactory());
        bean.setDataSource(dataSource);
        /*Properties a;
        (a = new Properties()).setProperty("", Global.getProperty("mybatis.dao"));
        bean.setConfigurationProperties(a);*/
        return bean;

    }

    @Bean
    public static MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        mapperScannerConfigurer.setBasePackage(Global.getProperty("mybatis.scanBasePackage"));
        mapperScannerConfigurer.setAnnotationClass(MyBatisDao.class);
        return mapperScannerConfigurer;
    }
}
